package com.tongda.menu.adapter;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.tongda.core.vo.DishesSetDishes;
import com.tongda.core.vo.DishesSetDishesReplace;
import com.tongda.menu.R;
import com.tongda.menu.activity.ShowActivity;
import com.tongda.menu.widget.HorizontalListView;

import android.content.Context;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CheckedTextView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class CartDsDishesAdapter extends BaseAdapter{
	
	private List<DishesSetDishes> datas;
	private Context context;
	List<DishesSetDishesReplace> dsReplaces;
	
	public CartDsDishesAdapter(List<DishesSetDishes> datas, Context context) {
		super();
		this.datas = datas;
		this.context = context;
	}

	@Override
	public int getCount() {
		return datas.size();
	}

	@Override
	public Object getItem(int position) {
		return datas.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		Holder holder = null;
		if(convertView==null){
			holder = new Holder();
			convertView = View.inflate(context, R.layout.adapter_dsdishes, null);
			holder.dsDisheTv = (TextView)convertView.findViewById(R.id.adapter_dsdishe_tv);
			holder.dsReplacesLv = (HorizontalListView)convertView.findViewById(R.id.adapter_dsreplaces_lv);
			convertView.setTag(holder);
		}else{
			holder = (Holder) convertView.getTag();
		}
		
		DishesSetDishes dsDishe = datas.get(position);
		holder.dsDisheTv.setText(dsDishe.getDishesName()+"x"+dsDishe.getUnitNum()+dsDishe.getUnitName());
		
		if((dsDishe.getDishesSetDishesReplaces()!=null && dsDishe.getDishesSetDishesReplaces().size()>0)){
			 dsReplaces= dsDishe.getDishesSetDishesReplaces();
			DsReplacesAdapter  dsReplaceAdapter = new DsReplacesAdapter(dsDishe,dsReplaces, context);
			holder.dsReplacesLv.setAdapter(dsReplaceAdapter);
			
			holder.dsReplacesLv.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View view,
						int subposition, long id) {
					DishesSetDishes dsDishe = datas.get(position);
					dsReplaces= dsDishe.getDishesSetDishesReplaces();
					DishesSetDishesReplace dsreplace = dsReplaces.get(subposition);
					
					String dsName = dsDishe.getDishesName();
					String dsId = dsDishe.getDishesId();
					String dsCode = dsDishe.getDishesCode();
					String dsNum = dsDishe.getUnitNum();
					String dsuName = dsDishe.getUnitName();
					
					String rpName = dsreplace.getDishesName();
					String rpId = dsreplace.getDishesId();
					String rpCode = dsreplace.getDishesCode();
					String rpNum = String.valueOf(dsreplace.getDsUnitNum());
					String rpuName = dsreplace.getDishesUnitName();
					
					dsDishe.setDishesName(rpName);
					dsDishe.setDishesId(rpId);
					dsDishe.setDishesCode(rpCode);
					dsDishe.setUnitNum(rpNum);
					dsDishe.setUnitName(rpuName);
					
					dsreplace.setDishesName(dsName);
					dsreplace.setDishesId(dsId);
					dsreplace.setDishesCode(dsCode);
					dsreplace.setDsUnitNum(new BigDecimal(dsNum));
					dsreplace.setDishesUnitName(dsuName);
					
					notifyDataSetChanged();
				}
			});
			
		}else{
			dsReplaces = new ArrayList<DishesSetDishesReplace>();
			DsReplacesAdapter  dsReplaceAdapter = new DsReplacesAdapter(datas.get(position),dsReplaces , context);
			holder.dsReplacesLv.setAdapter(dsReplaceAdapter);
		}
		return convertView;
	}

	public class Holder {
		TextView dsDisheTv;
		HorizontalListView dsReplacesLv;
		LinearLayout test;
	}
}
